

import java.util.List;

import javax.swing.table.AbstractTableModel;

public class MovieTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;

	private static final int COLS_NUM = 4;

	private List<Movie> movies;
	private String[] columnNames;

	public MovieTableModel(List<Movie> movies) {
		this.movies = movies;

		columnNames = new String[COLS_NUM];
		columnNames[0] = "ID";
		columnNames[1] = "Title";
		columnNames[2] = "Release date";
		columnNames[3] = "Thumbnail";
	}

	@Override
	public int getRowCount() {
		return movies.size();
	}

	@Override
	public int getColumnCount() {
		return COLS_NUM;
	}

	@Override
	public String getColumnName(int columnIndex) {
		return columnNames[columnIndex];
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return getValueAt(0, columnIndex).getClass();
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return movies.get(rowIndex).getId();

		case 1:
			return movies.get(rowIndex).getTitle();

		case 2:
			return movies.get(rowIndex).getDate();

		case 3:
			return movies.get(rowIndex).getThumbnailURL();
		}
		return null;
	}

	public void setMovies(List<Movie> movies) {
		this.movies = movies;
		fireTableDataChanged();
	}

}
